<?php
class Page_Tinymce_files extends Page
	{
	protected function build()
		{
		$f = $this->add(new Pages_Html('MYPAGE'));
		$f->setOrder('f_url asc',true);
		}
		
	protected function parse( &$tpl )
		{
		$tpl->setCondition('CALL_IS_FILE',($_GET['type'] == 'file' ));
		$tpl->setCondition('CALL_IS_IMAGE',($_GET['type'] == 'image' ));

		parent::parse($tpl);
		
		if($_GET['type'] == 'file')
			{
			$files = $this->getAllFiles(MEDIAS_ROOT.'pageshtml/');
			$tpl->startLoop('MYFILES');
			foreach($files as $file)
				{
				$tpl->newLoop('MYFILES');
				$tpl->setVarFile('MYFILES_FILE',$file,MEDIAS_ROOT.'pageshtml/');
				}
			$tpl->endLoop('MYFILES');
			}
		else
			{
			$files = $this->getAllPictures(MEDIAS_ROOT.'pageshtml/');
			$tpl->startLoop('MYPICTURES');
			foreach($files as $file)
				{
				$tpl->newLoop('MYPICTURES');
				$tpl->setVarFile('MYPICTURES_FILE',$file,MEDIAS_ROOT.'pageshtml/');
				}
			$tpl->endLoop('MYPICTURES');
			}
		}

   protected function getAllFiles($dirname)
      {
      $files = array();
      if( $handle = opendir( $dirname ) )
         {
         while( false !== ( $file = readdir( $handle ) ) )
            {
            if( $file != "." && $file != ".." && !is_dir( "$dirname/$file" ) )
            	{
            	$files[] = $file;
            	}
            }
         closedir( $handle );
         }
      asort($files);
      return $files;
      }

   protected function getAllPictures($dirname)
      {
      $files = array();
      if( $handle = opendir( $dirname ) )
         {
         while( false !== ( $file = readdir( $handle ) ) )
            {
            if( $file != "." && $file != ".." && !is_dir( "$dirname/$file" ) )
            	{
            	if(!getimagesize("$dirname/$file")) continue;
            	$files[] = $file;
            	}
            }
         closedir( $handle );
         }
      asort($files);
      return $files;
      }
		
	protected function getPageUrl() { return '/'; }
	protected function getBreadcrumbTitle() { return ''; }
	protected function getPageParent() { return ''; }
	}
?>